# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from AutoTestBDC.WorkFlow.User import User
from AutoTestBDC.extScript.GetList import GetListValue
from AutoTestBDC.extScript.SeaValue import SeaValue
from time import sleep

class ZS(User):
    def __init__(self,driver,username,userpwd,type=None,caseno=None,qlrtype=None):
        super().Login(driver, username, userpwd)
        self.driver = driver
        self.type = type
        self.caseno = caseno
        self.qlrtype = qlrtype



        self.test_tester()

    def test_tester(self,type=None):
        glv = GetListValue()
        if self.qlrtype == "自然人":
            tablexpath = "/html/body/content/div/form/div/div[19]"

        if self.qlrtype == "法人机构":
            tablexpath = "/html/body/content/div/form/div/div[20]"

        if self.qlrtype == "其他组织":
            tablexpath = "/html/body/content/div/form/div/div[21]"

        if self.type == "通过":
            #self.driver.find_element_by_css_selector(u"img[title=\"待审核\"]").click()
            self.driver.find_element_by_link_text(u"待审核").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_pass()

        if self.type == "不通过":
            self.driver.find_element_by_link_text(u"待审核").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_fail()

        if self.type =="分配入围机构":
            self.driver.find_element_by_link_text(u"审核中").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_fprwjg()

        if self.type == "退回初审":
            self.driver.find_element_by_link_text(u"审核中").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_backtocs()

        if self.type == "成果审核-不通过":
            self.driver.find_element_by_link_text(u"待审核").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_unqualified()

        if self.type == "成果审核-通过":
            self.driver.find_element_by_link_text(u"待审核").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_qualified()

        if self.type == "转发给前台":
            self.driver.find_element_by_link_text(u"审核中").click()
            SeaValue(self.driver, self.caseno, self.qlrtype)
            glv.clickvalue(self.driver, tablexpath, self.caseno)
            self.set_backtogt()

        super().Logout()

    def set_pass(self):
        self.driver.execute_script("arguments[0].scrollIntoView();", self.driver.find_element_by_id("detail-pass"))
        self.driver.find_element_by_id("detail-pass").click()
        sleep(1)
        self.driver.find_element_by_css_selector("button.modal_confirm").click()

    def set_fail(self):
        self.driver.find_element_by_xpath("/html/body/content/div/div[3]/div[4]/input").click()

        self.driver.find_element_by_css_selector(
            "div.nowrap > div.check > input[name=\"ifpass[]\"]").click()
        self.driver.find_element_by_id("reasonbzZS").clear()
        self.driver.find_element_by_id("reasonbzZS").send_keys(u"不通过")
        sleep(1)
        self.driver.find_element_by_css_selector("button.fail_submit_single").click()
        sleep(1)

        self.driver.execute_script("arguments[0].scrollIntoView();", self.driver.find_element_by_id("detail-fail"))
        self.driver.find_element_by_id("detail-fail").click()
        sleep(1)
        self.driver.find_element_by_css_selector("button.fail_submit").click()

    def set_fprwjg(self):
        self.driver.execute_script("arguments[0].scrollIntoView();", self.driver.find_element_by_id("detail-choose"))
        self.driver.find_element_by_id("detail-choose").click()
        sleep(1)
        self.driver.find_element_by_css_selector("input.checkbtn").click()
        self.driver.find_element_by_id("orgConfirm").click()
        sleep(1)
        self.driver.find_element_by_xpath("/html/body/div[2]/div/div/div[2]/div/button[1]").click()
        #self.driver.find_element_by_css_selector("button.modal_confirm_rw").click()

    def set_backtocs(self):
        self.driver.execute_script("arguments[0].scrollIntoView();",
                                   self.driver.find_element_by_id("detail-return"))
        self.driver.find_element_by_id("detail-return").click()
        sleep(1)

        #now_handle = self.driver.current_window_handle

        self.driver.find_element_by_css_selector("div.modal_container.modal_return > button.modal_confirm").click()
        #self.close_alert_and_get_its_text(self.driver,now_handle)

    def set_qualified(self):
        self.driver.execute_script("arguments[0].scrollIntoView();",
                                   self.driver.find_element_by_xpath("/html/body/content/div/div[49]/div[3]/button[3]"))

        self.driver.find_element_by_xpath("/html/body/content/div/div[49]/div[3]/button[3]").click()
        sleep(1)
        self.driver.find_element_by_css_selector("button.modal_confirm").click()

    def set_unqualified(self):
        self.driver.execute_script("arguments[0].scrollIntoView();",
                                   self.driver.find_element_by_xpath("/html/body/content/div/div[46]/div[1]"))

        self.driver.find_element_by_css_selector("#checkwork > input[name=\"ifpass[]\"]").click()
        sleep(1)
        self.driver.find_element_by_id("reasonworkZS").clear()
        self.driver.find_element_by_id("reasonworkZS").send_keys(u"成果提交的不符合要求")
        self.driver.find_element_by_css_selector("button.fail_submit_single").click()
        sleep(1)
        self.driver.find_element_by_id("detail-fail").click()
        sleep(2)
        self.driver.find_element_by_css_selector("button.fail_submit").click()

    def set_backtogt(self):
        self.driver.execute_script("arguments[0].scrollIntoView();",
                                   self.driver.find_element_by_id("detail-zhuanfa"))

        self.driver.find_element_by_id("detail-zhuanfa").click()


    def close_alert_and_get_its_text(self,driver,mainHandle):

        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            alert.dismiss()

            driver.switch_to_window(mainHandle)
        except Exception as e:
            print(e)
        finally:
            self.accept_next_alert = True
